﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EShop.BLL;
using EShop.Model;
using EShop.Common;

namespace EShop.Client
{
    public partial class frmMerchandiseBasicInfoEdit : frmBase
    {
        public frmMerchandiseBasicInfoEdit()
        {
            InitializeComponent();
        }

        private MerchandiseBasicInfo _MerchandiseBasicInfo;

        public MerchandiseBasicInfo MerchandiseBasicInfo
        {
            get{return _MerchandiseBasicInfo;}
            set{_MerchandiseBasicInfo = value;}
        }

        private bool _blISAdd = true;//是添加还是修改

        private static List<MerchandiseUnitInfo> aMerchandiseUnitInfoList;
        private static List<MerchandiseClass> aMerchandiseClassList;
        private string strBarCode = string.Empty;
        static frmMerchandiseBasicInfoEdit()
        {
            //取商品单位数据
            aMerchandiseUnitInfoList = new MerchandiseUnitBLL().GetAllMerchandiseUnitList();
            //取所有商品分类数据
            aMerchandiseClassList = new MerchandiseClassBLL().GetAllMerchandiseClassList();
        }

        private void MerchandiseBasicInfoEdit_Load(object sender, EventArgs e)
        {
            //取商品单位数据
            aMerchandiseUnitInfoList = new MerchandiseUnitBLL().GetAllMerchandiseUnitList();
            //取所有商品分类数据
            aMerchandiseClassList = new MerchandiseClassBLL().GetAllMerchandiseClassList();
            foreach (MerchandiseUnitInfo merchandiseUnitItem in aMerchandiseUnitInfoList)
            {
                cmb_MerchandiseUnit.Items.Add(merchandiseUnitItem);
            }

            foreach (MerchandiseClass merchandiseClassItem in aMerchandiseClassList)
            {
                cmb_MerchandiseType.Items.Add(merchandiseClassItem);
            }

            initBindData();
        }


        void initBindData()
        {
            if (_MerchandiseBasicInfo != null)
            {
                _blISAdd = false;
                strBarCode = _MerchandiseBasicInfo.Barcode;
                txtMerchandiseBarCode.Text = _MerchandiseBasicInfo.Barcode;
                txtMerchandiseName.Text = _MerchandiseBasicInfo.MerchandiseName;
                txtMerchandiseResume.Text = _MerchandiseBasicInfo.MerchandiseResume;
                txtMerchandiseRemark.Text = _MerchandiseBasicInfo.MerchandiseRemark;

                foreach (MerchandiseClass item in cmb_MerchandiseType.Items)
                {
                    if (item.MerchandiseClassID == _MerchandiseBasicInfo.MerchandiseClassID)
                    {
                        cmb_MerchandiseType.SelectedItem = item;
                        break;
                    }
                }

                foreach (MerchandiseUnitInfo item in cmb_MerchandiseUnit.Items)
                {
                    if (item.UnitID == _MerchandiseBasicInfo.MerchandiseUnitID)
                    {
                        cmb_MerchandiseUnit.SelectedItem = item;
                        break;
                    }
                }


                chkAlter.Checked = _MerchandiseBasicInfo.IsAlert;
                txtAlterNum.Text = _MerchandiseBasicInfo.AlertNum.ToString();
                chkHasOverTime.Checked = _MerchandiseBasicInfo.HasOverTime;
            }
            else
            {
                txtMerchandiseBarCode.Enabled = true;
                txtMerchandiseBarCode.Focus();
            }
        }

        private void btnConform_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtMerchandiseName.Text) || cmb_MerchandiseType.SelectedItem == null || cmb_MerchandiseUnit.SelectedItem == null)
            {
                MessageBox.Show("请输入必选项!");
                this.DialogResult = DialogResult.No;
                return; 
            }

            _MerchandiseBasicInfo = new MerchandiseBasicInfo();
            _MerchandiseBasicInfo.Barcode = SystemLibrary.SQLQuoteName(txtMerchandiseBarCode.Text);
            _MerchandiseBasicInfo.MerchandiseName = SystemLibrary.SQLQuoteName(txtMerchandiseName.Text);

            if (cmb_MerchandiseType.SelectedItem != null)
            {
                _MerchandiseBasicInfo.MerchandiseClassID = ((MerchandiseClass)cmb_MerchandiseType.SelectedItem).MerchandiseClassID;
                _MerchandiseBasicInfo.MerchandiseClassName = ((MerchandiseClass)cmb_MerchandiseType.SelectedItem).MerchandiseClassName;

            }
            if (cmb_MerchandiseUnit.SelectedItem != null)
            {
                _MerchandiseBasicInfo.MerchandiseUnitID = ((MerchandiseUnitInfo)cmb_MerchandiseUnit.SelectedItem).UnitID;
                _MerchandiseBasicInfo.MerchandiseUnitName = ((MerchandiseUnitInfo)cmb_MerchandiseUnit.SelectedItem).UnitName;
            }
            _MerchandiseBasicInfo.MerchandiseResume = SystemLibrary.SQLQuoteName(txtMerchandiseResume.Text);
            _MerchandiseBasicInfo.MerchandiseRemark = SystemLibrary.SQLQuoteName(txtMerchandiseRemark.Text);
            _MerchandiseBasicInfo.IsAlert = chkAlter.Checked;
            //if (chkAlter.Checked)
                _MerchandiseBasicInfo.AlertNum = Convert.ToInt32(txtAlterNum.Value);
            _MerchandiseBasicInfo.HasOverTime = chkHasOverTime.Checked;


            Merchandise_BasicInfoBLL oBaseInfoBLL=new Merchandise_BasicInfoBLL();
            if(_blISAdd)
            {
                if (!oBaseInfoBLL.Add(_MerchandiseBasicInfo))
                {
                    MessageBox.Show("添加商品数据失败，请确认数据库中无该商品！");
                    this.DialogResult = DialogResult.No;
                }
            }
            else
            {
                if(!oBaseInfoBLL.Update(strBarCode, _MerchandiseBasicInfo))
                {
                    MessageBox.Show("修改商品数据失败！");
                    this.DialogResult = DialogResult.No;
                }

            }

        }

        private void frmMerchandiseBasicInfoEdit_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult == DialogResult.No)
                e.Cancel = true;
        }

        private void chkAlter_CheckedChanged(object sender, EventArgs e)
        {
            txtAlterNum.Enabled = chkAlter.Checked;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            txtMerchandiseBarCode.Enabled = true;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {

        }
    }
}
